package com.jksoft.darewayyyy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jksoft.darewayyyy.R;
import com.jksoft.darewayyyy.pojo.Employee;
import com.jksoft.darewayyyy.util.FileUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2015/10/9.
 */
public class EmployeeAdapter extends RecyclerView.Adapter<EmployeeAdapter.ViewHolder> implements View.OnClickListener{
    private List<Employee> employeeList;
    private LayoutInflater inflater;
//    private ImageLoader imageLoader;
    private FileUtils fileUtils;
    private RecyclerViewOnItemClickListener mListener;
    private Context context;
    public EmployeeAdapter(Context context, List<Employee> employeeList) {
        this.employeeList = employeeList;
        this.context = context;
        inflater = LayoutInflater.from(context);
//        imageLoader = ImageLoader.getInstance();
        fileUtils = new FileUtils(context);
    }

    public void setRecyclerViewOnItemClickListener(RecyclerViewOnItemClickListener recyclerViewOnItemClickListener) {
        mListener = recyclerViewOnItemClickListener;
    }


    public void addItem(Employee employee) {
        employeeList.add(0, employee);
        notifyItemInserted(0);
    }

    public void removeItem(Employee employee) {
        int position = employeeList.indexOf(employee);
        employeeList.remove(employee);
        notifyItemRemoved(position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_recyclerview, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Employee employee = employeeList.get(position);
        holder.username.setText(employee.getEmpName());
        holder.phonetype.setText(employee.getPhoneType());
        Glide.with(context)
                .load(employee.getImgUrl())
                .placeholder(R.mipmap.defaultphoto)
                .into(holder.photo);
//        holder.photo.setImageBitmap();
//        imageLoader.displayImage(employee.getImgUrl(), holder.photo);

        holder.itemView.setTag(employee);
    }

    @Override
    public int getItemCount() {
        return employeeList.size();
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.onItemClickListener(v, (Employee) v.getTag());
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView photo;
        private TextView username, phonetype;
        private View itemView;
        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            photo = (ImageView) itemView.findViewById(R.id.photo);
            username = (TextView) itemView.findViewById(R.id.username);
            phonetype = (TextView) itemView.findViewById(R.id.phonetype);
        }
    }

    public static interface RecyclerViewOnItemClickListener {
        void onItemClickListener(View v, Employee employee);
    }
}
